package com.smart_campus.teg.controller;

import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.User;
import com.smart_campus.teg.domain.TegDormitory;
import com.smart_campus.teg.service.TegDormitoryService;
import com.smart_campus.teg.vo.DormitoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("dormi")
@RestController
public class DormitoryController {

    @Autowired
    TegDormitoryService dormitoryService;

    @RequestMapping("selDormitory")
    public DataGridView selDormitory(DormitoryVo dormitory){
        return dormitoryService.selDorimtory(dormitory);
    }

    @RequestMapping("addDormitory")
    public ResultObj addDormitory(TegDormitory tegDormitory){
        Integer dormitoryStaff = dormitoryService.selBydormitoryNumber(tegDormitory);
        //添加时判断是否存在该学生
        Boolean b = dormitoryService.existsStudent(tegDormitory);
        if (!b){
            return new ResultObj(-1,"没有该学生或没有该班级或该班级下没有该学生");
        }
        User user = dormitoryService.selByStudengName(tegDormitory.getStudentName());
        tegDormitory.setStudentPhone(user.getPhone());
        if (dormitoryStaff!=null){
            if (dormitoryStaff == tegDormitory.getDormitoryBeds()){
                return new ResultObj(-1,"添加失败该宿舍人员已满");
            }
        }else {
            dormitoryStaff = 0;
        }

        tegDormitory.setCreateTime(new Date());
        tegDormitory.setUpdateTime(new Date());
        tegDormitory.setDormitoryStaff(dormitoryStaff+1);
        dormitoryService.updatedormitoryStaff(tegDormitory);
        dormitoryService.insert(tegDormitory);
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("updateDormitory")
    public ResultObj updateDormitory(TegDormitory tegDormitory){
        try {
            //查询修改前的数据
            TegDormitory dormitory = dormitoryService.selectByPrimaryKey(tegDormitory.getId());
              //如果修改后的宿舍和修改前的宿舍不一致修改人员
            if (dormitory.getDormitoryNumber() != tegDormitory.getDormitoryNumber()){//判断修改后是否与修改前的宿舍是否一致
                dormitory.setDormitoryStaff(dormitory.getDormitoryStaff()-1);//修改前的宿舍-1
                dormitoryService.updatedormitoryStaff(dormitory);
                TegDormitory dormitory1 = dormitoryService.selupdateBydormitoryNumber(tegDormitory);
                if (dormitory1!=null){//如果是修改一个没人的宿舍
                    dormitory1.setDormitoryStaff(dormitory1.getDormitoryStaff()+1);//修改后的宿舍+1
                    dormitoryService.updatedormitoryStaff(dormitory1);
                    tegDormitory.setDormitoryStaff(dormitory1.getDormitoryStaff());
                }else {
                    tegDormitory.setDormitoryStaff(1);
                }
            }
            tegDormitory.setUpdateTime(new Date());
            dormitoryService.updateByPrimaryKey(tegDormitory);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteDormitory")
    public ResultObj deleteDormitory(int id[]){
        try {
            for (int i: id){
                TegDormitory tegDormitory = dormitoryService.selectByPrimaryKey(i);
                tegDormitory.setDormitoryStaff(tegDormitory.getDormitoryStaff()-1);
                dormitoryService.updatedormitoryStaff(tegDormitory);
                dormitoryService.deleteByPrimaryKey(i);
            }
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }


    }

}
